<?php
namespace Wap\Logic;
use Think\Model;

class EventRuleLogic extends Model
{
    protected $tableName = 'organiztion_record';

    private $day_num = 4;//每天可以给多少人投票
    private $days_num = 1;

    private $per_day_num = 1;//每天可以给一个人投几票
    private $per_days_num = 1;//活动期间内，可以给一个人重复投几次

    public function __construct( $config )
    {
        $this->sum_num = isset( $config['sum_num'] ) ? $config['sum_num'] : -1;
        $this->day_num = isset( $config['day_num'] ) ? $config['day_num'] : -1;
        $this->per_num = isset( $config['per_num'] ) ? $config['per_num'] : -1;
        $this->per_day_num = isset( $config['per_day_num'] ) ? $config['per_day_num'] : -1;
        parent::__construct();
    }


    public function isGoodDayNum( $event, $openid )
    {
        $limit = $this->day_num;
        if( $limit === -1 )
        {
             return true;
        }

        $unixtime = time();
        $today = array(
            mktime( 0, 0, 0, date( 'm', $unixtime ), date( 'd', $unixtime ), date( 'Y', $unixtime ) ),
            mktime( 23, 59, 59, date( 'm', $unixtime ), date( 'd', $unixtime ), date( 'Y', $unixtime ) )
        );

        $conditions = array(
            'event_id' => $event,
            'openid' => $openid,
            'update_time' => array( 'BETWEEN', $today )
        );

        $count = $this->distinct( true )->field( 'organiztion_id' )->where( $conditions )->count();

        return ($limit - $count);
    }

    public function isGoodPerDayNum( $organiztion, $openid )
    {
        $limit = $this->per_day_num;
        if( $limit === -1 )
        {
             return true;
        }

        $unixtime = time();
        $today = array(
            mktime( 0, 0, 0, date( 'm', $unixtime ), date( 'd', $unixtime ), date( 'Y', $unixtime ) ),
            mktime( 24, 59, 59, date( 'm', $unixtime ), date( 'd', $unixtime ), date( 'Y', $unixtime ) )
        );

        $conditions = array(
            'organiztion_id' => $organiztion,
            'openid' => $openid,
            'update_time' => array( 'BETWEEN', $today )
        );

        $count = $this->where( $conditions )->count();

        return ( $limit - $count );
    }

    public function isGoodDaysNum()
    {
        return true;
    }

    public function isGoodPerDaysNum( $organiztion, $openid )
    {
        $limit = $this->per_days_num;
        if( $limit === -1 )
        {
             return true;
        }

        $conditions = array(
            'organiztion_id' => $organiztion,
            'openid' => $openid
        );

        $count = $this->where( $conditions )->count();

        return ( $limit - $count );
    }
}
?>
